from setting import *
from gamebackground import *
from heroplane import *
from selecthero import *
from enemyplane import *
from supply import *

class MainScene(object):
    #初始化
    def __init__(self):
        pygame.mixer.pre_init()
        pygame.init()
        pygame.mixer.init()
        pygame.font.init()
        # pygame.mouse.set_visible(False)
        self.scene = pygame.display.set_mode((SCENEWIDTH, SCENEHEIGHT), 1, 32)
        # self.scene = pygame.display.set_mode((SCENEWIDTH, SCENEHEIGHT), pygame.FULLSCREEN, 32)
        pygame.display.set_caption("自学飞机大战--精灵组版")
        #载入地图
        b = SelectHero(self.scene)
        b.run()
        hero_index = b.hero_index
        b.kill()
        self.bground = Background(self.scene)

        self.hero = HeroPlane(hero_index)
        self.hero.set_bullet_type(hero_index)
        self.hero.set_fire_type(0)

        # print(self.hero.speed, self.hero.damage, self.hero.HP)
        self.hero.set_pos(SCENEWIDTH // 2 - 50, SCENEHEIGHT - 100)

        pygame.time.set_timer(CREATE_ENEMY_TIME,100)

    #绘制各个元素
    def draw(self):
        self.bground.draw()
        allgroup.draw(self.scene)

    def update(self):
        self.bground.update()
        allgroup.update()

    def collideEvent(self):
        hero_shot_enemy = pygame.sprite.groupcollide(herobulletgroup,enemygroup,True,True)
        if hero_shot_enemy:
            #hero_shot_enemy为子弹 key为 子弹,value 为 飞机
            for v in hero_shot_enemy:
                #获得 key 的 values,
                #即获得当前子弹射中的敌机列表
                enemys = hero_shot_enemy[v]
                #遍历敌机
                for enemy in enemys:
                    #用随机函数控制掉率
                    if random.randint(0,1) == 0:
                        supply_pos = enemy.rect.center
                        supply = Supply()
                        supply.set_pos(supply_pos[0],supply_pos[1])
        hero_collide_supply = pygame.sprite.spritecollide(self.hero,supplygroup,True)
        if hero_collide_supply:
            self.hero.fire_type += 1

    #处理键盘和鼠标相应
    def handleEvent(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_j:
                    #普通射击
                    self.hero.fire_key_down(pygame.K_j)
                    # self.hero.fire_bullet()
                elif event.key == pygame.K_a:
                    self.hero.key_down(pygame.K_a)
                elif event.key == pygame.K_d:
                    self.hero.key_down(pygame.K_d)
                elif event.key == pygame.K_w:
                    self.hero.key_down(pygame.K_w)
                elif event.key == pygame.K_s:
                    self.hero.key_down(pygame.K_s)
                #ESC 退出jjj
                elif event.key == pygame.K_ESCAPE or event.key == pygame.K_F4:
                    pygame.quit()
                    exit()
            #按键松开
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_j:
                    self.hero.fire_key_up(pygame.K_j)
                elif event.key == pygame.K_a:
                    self.hero.key_up(pygame.K_a)
                elif event.key == pygame.K_d:
                    self.hero.key_up(pygame.K_d)
                elif event.key == pygame.K_w:
                    self.hero.key_up(pygame.K_w)
                elif event.key == pygame.K_s:
                    self.hero.key_up(pygame.K_s)
            elif event.type == CREATE_ENEMY_TIME:
                self.createEnemy()

        self.hero.press_move()
        self.hero.press_fire()
    def createEnemy(self):
        if random.randint(0,3)==1:
            enemy = EnemyPlane()
            enemy.set_pos(random.randint(0,SCENEWIDTH)+50,-20)

    def runScene(self):
        # pygame.mixer.music.load('music/bgm.mp3')
        # pygame.mixer.music.play(-1)
        clock = pygame.time.Clock()
        while True:
            clock.tick(60)
            self.draw()
            self.update()
            self.collideEvent()
            self.handleEvent()
            pygame.display.flip()


if __name__ == "__main__":
    play = MainScene()
    play.runScene()

else:
    print("i was imported by", __name__)
